import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Error from '../views/Error.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [//重定向
    {
      path: '/',
      redirect: 'Home'
    },
    {
      path:'/home',
      component: Home
    },
    {
      path:'/login',
      component: Login
    },
    { 
      path: '/:pathMatch(.*)*',
      component: Error
    },
  ]
})
//守卫
router.beforeEach((to, from) => {
  if(to.path!='/login'){
    const token=localStorage.getItem('token')
    if(!token){
      return {
        path:"/login"
      }
    }
  }
})
export default router
